﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Z2102WinForms.SY
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
            panel1.Visible = false;
            buttonConsole.Click += delegate
            {
                Program.ExecConsoleApp("Z2102Console.SY.Class1");
            };
            buttonWinForms.Click += delegate
            {
                panel1.Visible = true;
                ShowResult();
            };
        }

        private void ShowResult()
        {
            string s = textBox1.Text;
            if (s.Length == 0)
            {
                label2.Text = "错误：输入的字符串无法转换为整数！";
                return;
            }
            s = s.Trim();
            if (int.TryParse(s, out int n) == false)
            {
                label2.Text = "错误：输入的字符串无法转换为整数！";
                return;
            }
            if (n <= 100)
            {
                label2.Text = "错误：输入的整数不满足大于100的条件！";
                return;
            }
            label2.Text = string.Format("该整数共有{0}位。\r\n", s.Length);

            //方式1--字符提取法
            label2.Text += GetResult(s) + "\r\n";

            //方式2--整数整除法
            label2.Text += GetResult(n);
        }
        private static string GetResult(string s)
        {
            string s1 = "";
            int n1 = 0;
            for (int i = 0; i < s.Length; i++)
            {
                s1 += s[i] + "、";
                n1 += int.Parse(s[i].ToString());
            }
            return $"字符提取法：每一位的值为{s1.TrimEnd('、')}，这些位之和为{n1}";
        }
        private static string GetResult(int n)
        {
            int length = n.ToString().Length;
            int n1 = 0;
            string s1 = "";
            for (int i = length; i > 0; i--)
            {
                int k = int.Parse("1" + new string('0', i - 1));
                s1 += n / k + "、";
                n1 += n / k;
                n %= k;
            }
            return $"整数整除法：每一位的值为{s1.TrimEnd('、')}，这些位之和为{n1}";
        }
    }
}
